<template>
  <div>
    <!-- 经销商批售车型授权 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="zjsaleAuthorization"
        :onRowSelectChange="selectChangeHandle"
        :isSelectColumn="false"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(2)">导 入</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(3)">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="visible" :title="'基本信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <childrenModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="addvisible" :title="'基本信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addModel :rowData="formData" @close="addcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isSearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownerclose" />
    </BaseDialog>
    <BaseDialog :visible.sync="isImport" :title="'导入'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <importFiles @close="importcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isproduct" :title="'产品组查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <productSeacher @close="closeHandlerProduct" @addchange="addChangeProduct" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, getDetail, postSave, getDealerUser, exportList ,exporturl} from '@/api/omSale/vehicleParameters/saleAuthorization/index.js';
import childrenModel from './childrenModel';
import ownerSeacher from './ownerSeacher';
import addModel from './addModel';
import importFiles from './importFiles';
import productSeacher from './productSeacher';
import { exportExcelFile } from '@/utils';
export default {
  name: 'businessParameters',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        xhrAbort: true
      },
      formData: {},
      exportLoading: false,
      visible: false,
      addvisible: false,
      isSearch: false,
      isImport: false,
      isproduct: false,
      groupId: ''
    };
  },
  mixins: [dictionary],
  components: {
    childrenModel,
    addModel,
    ownerSeacher,
    importFiles,
    productSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getUser();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '产品组名称',
          fieldName: 'groupName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.groupId = '';
            }
          }
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'startDate|endDate',
          placeholder: '请选择',
          itemList: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003')
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'division',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'office',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品组代码',
          dataIndex: 'groupCode',
          sorter: true,
          render: p => {
            let arr = p.row.groups;
            return (
              <div>
                {arr.map(v => {
                  return <span>{v.groupCode} </span>;
                })}
              </div>
            );
          }
        },
        {
          title: '产品组名称',
          dataIndex: 'groupName',
          sorter: true,
          render: p => {
            let arr = p.row.groups;
            return (
              <div>
                {arr.map(v => {
                  return <span>{v.groupName} </span>;
                })}
              </div>
            );
          }
        },
        {
          title: '启用标记',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: p => {
            return <span>{this.createDictText(p.row.status, '1003')}</span>;
          }
        },

        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {},
    async addHandle(type) {
      if (type == 1) {
        this.addvisible = true;
      } else if (type == 2) {
        this.isImport = true;
      } else {



          let json = Object.assign({}, { CODE: 'CODE_0000024' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }


      //   this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //     confirmButtonText: '下载',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   })
      //     .then(() => {
      //       let json = this.fetchParams;
      //       this.exportLoading = true;
      //       exportList(json)
      //         .then(response => {
      //           console.log(response, 'response');
      //           exportExcelFile(response.data, '批售车型授权');
      //           this.exportLoading = false;
      //         })
      //         .catch(err => {
      //           console.log(err);
      //         });
      //     })
      //     .catch(() => {
      //       console.log('456');
      //       this.$message({
      //         type: 'info',
      //         message: '已取消'
      //       });
      //     });
       }
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams.groupId = this.groupId;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.visible = true;
    },
    addcloseHandler(val) {
      this.addvisible = false;
      if (val) {
        this.fetchParams.groupId = this.groupId;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    emitcloseHandler(val) {
      this.visible = false;
      if (val) {
        this.fetchParams.groupId = this.groupId;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    ownerclose(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.isSearch = false;
    },
    seacherFun(type) {
      if (type == 1) {
        this.isproduct = true;
      } else {
        this.isSearch = true;
      }
    },
    resetChange(val) {
      this.groupId = '';
      this.fetchParams.groupId = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    importcloseHandler() {
      this.isImport = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'createdBy').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'createdBy').filterItems = empList;
    },
    addChangeProduct(val) {
      if (val) {
        let arr = [],
          arrs = [],
          str = '',
          strs = '';
        val.map(x => {
          arr.push(x.groupId);
          arrs.push(x.groupName);
        });
        str = arr.join(',');
        strs = arrs.join(',');
        this.topFilterList.find(x => x.fieldName == 'groupName').initialValue = strs;
        this.groupId = str;
      }
      this.isproduct = false;
    },
    closeHandlerProduct() {
      this.isproduct = false;
    }
  }
};
</script>
